package com.kingyea.mobilepolice.systemsync;

import com.alibaba.fastjson.JSON;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.systemsync.bean.InvokedMethod;
import com.kingyea.mobilepolice.util.GlobalVar;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 内网对外暴露的同步接口
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/3/1 17:51
 */
@RequestMapping(value = "/syssync")
@RestController
public class SystemSyncController {

    /**
     * @Description: 内网接受需要同步的数据 并且 把外网需要同步的数据返回
     * @Param: [resultStr]
     * @return: java.lang.Object
     * @Author: cxc
     * @Date: 2018.04.20 11:04
     */
    @PostMapping("/queue")
    public Object inputQueue(@RequestBody Result<List<InvokedMethod>> result) {
        //内网处理需要同步的数据
        if (GlobalVar.inside.equals(GlobalVar.insideNei)) {
            if (result != null && StateCode.OPERATION_SUCCESS.equals(result.getCode())
                    && result.getData() != null && result.getData().size() != 0) {
                // 入列
                System.out.println("【入列】" + JSON.toJSONString(result));
                result.getData().stream().forEach(method -> SyncThread.toBeInvokSyncQueue.offer(method));
            }
            // 把外网需要同步的数据返回 (包括普通同步和token同步数据)
            List<Object> list = ActiveMqUtils.getInvokeMethodList(GlobalVar.MQ_USERNAME, GlobalVar.MQ_PASSWORD, GlobalVar.MQ_BROKER_URL, GlobalVar.MQ_SUBJECT);
            list.addAll(ActiveMqUtils.getInvokeMethodList(GlobalVar.MQ_USERNAME, GlobalVar.MQ_PASSWORD, GlobalVar.MQ_BROKER_URL, GlobalVar.MQ_SUBJECT_TOKEN));
            System.out.println("【返回同步数据】" + list);
            return Result.getSuccessResult(list.stream().map(bean -> JSON.parseObject(((String) bean), InvokedMethod.class)).collect(Collectors.toList()));//HttpUtil.invokeUrl(GlobalVar.insideAddress + "/uaa/syssync/queue?access_token=0aec4252-5622-4d92-9b61-fd2436ef6a7b", new HashMap(), new HashMap<>(), HttpMethod.GET);
        }
        return Result.getFailedResult(" 非法访问 外网 不提供同步信息 ");
    }


}